/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view;

import com.aeky.client.common.AppParams;
import com.aeky.client.common.DefaultCallback;
import com.aeky.client.rpcservice.GreetingRPCService;
import com.aeky.client.rpcservice.GreetingRPCServiceAsync;
import com.aeky.client.view.base.AekyWidget;
import com.aeky.client.view.main.MainApplication;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Demo implements EntryPoint, ValueChangeHandler<String> {

    private final GreetingRPCServiceAsync greetingService = AppParams.getService(GreetingRPCService.class);

    public void onModuleLoad() {
        History.addValueChangeHandler(this);
        greetingService.greetServer("aeky", new DefaultCallback<String>() {
            public void onSuccess(String msg) {
                super.onSuccess(msg);
            }

            public void onFailure(Throwable caught) {
                super.onFailure(caught);
            }
        });
    }

    /*
     * 
     * @see
     * com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(
     * com.google.gwt.event.logical.shared.ValueChangeEvent)
     */
    public void onValueChange(ValueChangeEvent<String> event) {
        if (!(AppParams.getInstance().getApp() instanceof MainApplication)) {
            return;
        }
        final String token = event.getValue();
        final AekyWidget widget = AppParams.getInstance().findTokenWidget(token);
        final AekyWidget currentWidget = AppParams.getInstance().getCurrentWidget();
        this.doValueChange(currentWidget, widget, token);
    }

    private void doValueChange(final AekyWidget currentWidget, AekyWidget widget, String token) {
        if (currentWidget.equals(widget)) {
            AppParams.getInstance().getApp().getContentPanel().show();
            return;
        }
        AppParams.getInstance().getApp().setContent(widget);
    }
}
